package com.application.Activity;

import java.io.IOException;
import java.util.ArrayList;

import com.application.Controller.ActivityController;
import com.application.Controller.ReadWriteFileController;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageButton;

public class FrmNote extends Activity implements OnClickListener,OnKeyListener {
	private String _fileName="note_mlearning.txt";
	EditText _edtcontent;
	ImageButton _btnclear;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_note);
		_edtcontent=(EditText)findViewById(R.id.note_edtcontent);
		_btnclear=(ImageButton) findViewById(R.id.note_clearall);
		_edtcontent.setOnKeyListener(this);
		
		try {
			readNote();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		_btnclear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				_edtcontent.setText("");
				ReadWriteFileController.deleteFile(ReadWriteFileController.DIRECTORY + _fileName);
			}
		});
		
		
	}
	private void readNote() throws IOException{
		ArrayList<String> contentNote = new ArrayList<String>();
		contentNote=ReadWriteFileController.ReadTXT(ReadWriteFileController.DIRECTORY + _fileName);
		String temp="";
		for(int i=0;i<contentNote.size();i++){
			temp+=contentNote.get(i).toString()+"\n";
		}
		_edtcontent.setText(temp);
	}
	private void writeNote() throws IOException{
		ReadWriteFileController.deleteFile(ReadWriteFileController.DIRECTORY + _fileName);
		String content = _edtcontent.getText().toString();
		ReadWriteFileController.writeTXT(_fileName, content);
	}
	/**
	 * bat su kien enter khi chat
	 * @param v
	 * @param keyCode
	 * @param event
	 * @return
	 */
//	public boolean onKey(View v, int keyCode, KeyEvent event) {
//	    switch (keyCode) {
//	        case KeyEvent.KEYCODE_BACK:
//			try {
//				writeNote();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//	      return true;
//	    }
//	    return false;
//	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		try {
			writeNote();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		ActivityController.setPeerActivity(this);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		try {
			writeNote();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
		try {
			readNote();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	
}
